package com.cqu.hhx.driver.controller;

import com.cqu.hhx.driver.entity.*;
import com.cqu.hhx.driver.entity.forWeb.AccountAndDate;
import com.cqu.hhx.driver.entity.forWeb.ChangeReq;
import com.cqu.hhx.driver.service.ManageService;
import com.cqu.hhx.driver.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DJ
 * @create 2020-05-26 19:12
 */
@RestController
public class ManageController {
    @Autowired
    private ManageService manageService;
    @Autowired
    private OrderService orderService;

    /**
     * 修改乘客信息
     * @param customerId
     * @param changeReq
     * @return 0:成功 1:失败
     */
    @RequestMapping(value = "/manage/change/customer/{customerId}",method = RequestMethod.POST)
    public Integer changeCustomerInfo(@PathVariable Long customerId, @RequestBody ChangeReq changeReq){
        Integer status=manageService.updateCustomer(customerId,changeReq);
        return status;
    }

    /**
     * 修改司机信息
     * @param driverId
     * @param changeReq
     * @return 0:成功 1:失败
     */
    @RequestMapping(value = "/manage/change/driver/{driverId}",method = RequestMethod.POST)
    public Integer changeDriverInfo(@PathVariable Long driverId,@RequestBody ChangeReq changeReq){
        Integer status=manageService.updateDriver(driverId,changeReq);
        return status;
    }


    /**
     * 删除乘客
     * @param customerId
     * @return 0:成功 1:失败
     */
    @RequestMapping(value = "/manage/delete/customer/{customerId}",method = RequestMethod.GET)
    public Integer deleteCustomerById(@PathVariable Long customerId){
        Integer status=manageService.deleteCustomer(customerId);
        return status;
    }

    /**
     * 删除司机
     * @param driverId
     * @return 0:成功 1:失败
     */
    @RequestMapping(value = "/manage/delete/driver/{driverId}",method = RequestMethod.GET)
    public Integer deleteDriverById(@PathVariable Long driverId){
        Integer status=manageService.deleteDriver(driverId);
        return status;
    }

    /**
     * 重置乘客密码
     * @param customerId
     * @return 0:成功 1:失败
     */
    @RequestMapping(value = "/manage/reset/customer/{customerId}/password",method = RequestMethod.GET)
    public Integer resetCustomerPassword(@PathVariable Long customerId){
        Integer status=manageService.resetCustomerPassword(customerId);
        return status;
    }

    /**
     * 重置司机密码
     * @param driverId
     * @return 0:成功 1:失败
     */
    @RequestMapping(value = "/manage/reset/driver/{driverId}/password",method = RequestMethod.GET)
    public Integer resetDriverPassword(@PathVariable Long driverId){
        Integer status=manageService.resetDriverPassword(driverId);
        return status;
    }

    /**
     * 通过手机号获取乘客信息
     * @param phone
     * @return
     */
    @RequestMapping(value = "/customer/get/info/by/{phone}",method = RequestMethod.GET)
    public Map<String,Object> getCustomerInfoByPhone(@PathVariable String phone){
        Map<String,Object> map=new HashMap<>();
        if(phone.length()==11){
            Customer customer=manageService.getCustomerInfoByPhone(phone);
            if(customer!=null){
                map.put("status",0);
                map.put("customer",customer);
            }else{
                map.put("status",1);
            }
        }else{
            map.put("status",1);
        }
        return map;
    }

    /**
     * 通过手机号获取司机信息
     * @param phone
     * @return
     */
    @RequestMapping(value = "/driver/get/info/by/{phone}",method = RequestMethod.GET)
    public Map<String,Object> getDriverInfoByPhone(@PathVariable String phone){
        Map<String,Object> map=new HashMap<>();
        if(phone.length()==11){
            Driver driver=manageService.getDriverInfoByPhone(phone);
            if(driver!=null){
                map.put("status",0);
                map.put("driver",driver);
            }else{
                map.put("status",1);
            }
        }else{
            map.put("status",1);
        }
        return map;
    }

    /**
     * 获取乘客已完成订单列表
     * @param accountAndDate
     * @return
     */
    @RequestMapping(value = "/manager/get/customer/finish/order/",method = RequestMethod.POST)
    public Map<String,Object> getCustomerFinishedOrderList(
            @RequestBody AccountAndDate accountAndDate){
        Map<String,Object> map=new HashMap<>();
        Customer customer=manageService.getCustomerInfoByPhone(accountAndDate.getAccount());
        Date selectedDay=new Date(accountAndDate.getSelectedDay());
        if(customer!=null){
            List<FinishOrder> orders=manageService.getCustomerFinishedOrder(selectedDay,customer.getCustomerId());
            if(orders!=null){
                map.put("status",0);
                map.put("customerName",customer.getCustomerName());
                map.put("orders",orders);
            }else{
                map.put("status",2);
            }
        }else{
            map.put("status",1);
        }
        return map;
    }

    /**
     * 获取乘客进行中订单列表
     * @param account
     * @return
     */
    @RequestMapping(value = "/manager/get/customer/doing/order/{account}",method = RequestMethod.GET)
    public Map<String,Object> getCustomerDoingOrderList(@PathVariable String account){
        Map<String,Object> map=new HashMap<>();
        Customer customer=manageService.getCustomerInfoByPhone(account);
        if(customer!=null){
            DoingOrder orders=orderService.customerGetDoingOrder(customer.getCustomerId());
            if(orders!=null){
                map.put("status",0);
                map.put("customerName",customer.getCustomerName());
                map.put("orders",orders);
            }else{
                map.put("status",2);
            }
        }else{
            map.put("status",1);
        }
        return map;
    }

    /**
     * 获取乘客新下单订单列表
     * @param account
     * @return
     */
    @RequestMapping(value = "/manager/get/customer/new/order/{account}",method = RequestMethod.GET)
    public Map<String,Object> getCustomerNewOrderList(@PathVariable String account){
        Map<String,Object> map=new HashMap<>();
        Customer customer=manageService.getCustomerInfoByPhone(account);
        if(customer!=null){
            NewOrder orders=orderService.customerGetNewOrder(customer.getCustomerId());
            if(orders!=null){
                map.put("status",0);
                map.put("customerName",customer.getCustomerName());
                map.put("orders",orders);
            }else{
                map.put("status",2);
            }
        }else{
            map.put("status",1);
        }
        return map;
    }


    /**
     * 获取司机进行中订单列表
     * @param account
     * @return
     */
    @RequestMapping(value = "/manager/get/driver/doing/order/{account}",method = RequestMethod.GET)
    public Map<String,Object> getDriverDoingOrderList(@PathVariable String account){
        Map<String,Object> map=new HashMap<>();
        Driver driver=manageService.getDriverInfoByPhone(account);
        if(driver!=null){
            DoingOrder orders=orderService.driverGetDoingOrder(driver.getDriverId());
            if(orders!=null){
                Customer customer=manageService.getCustomerInfoById(orders.getCustomerId());
                map.put("status",0);
                map.put("customerName",customer.getCustomerName());
                map.put("orders",orders);
            }else{
                map.put("status",2);
            }
        }else{
            map.put("status",1);
        }
        return map;
    }

    /**
     * 获取司机已完成订单列表
     * @param accountAndDate
     * @return
     */
    @RequestMapping(value = "/manager/get/driver/finish/order/",method = RequestMethod.POST)
    public Map<String,Object> getDriverFinishedOrderList(
            @RequestBody AccountAndDate accountAndDate){
        Map<String,Object> map=new HashMap<>();
        Driver driver=manageService.getDriverInfoByPhone(accountAndDate.getAccount());
        Date selectedDay=new Date(accountAndDate.getSelectedDay());
        if(driver!=null){
            List<FinishOrder> orders=manageService.getDriverFinishedOrder(selectedDay,driver.getDriverId());
            List<String> customers=new ArrayList<>();
            if(orders!=null){
                for(FinishOrder order:orders){
                    Customer customer=manageService.getCustomerInfoById(order.getCustomerId());
                    customers.add(customer.getCustomerName());
                }
                map.put("status",0);
                map.put("customers",customers);
                map.put("orders",orders);
            }else{
                map.put("status",2);
            }
        }else{
            map.put("status",1);
        }
        return map;
    }

    /**
     * 获取选择日期的所有订单
     * @param accountAndDate
     * @return
     */
    @RequestMapping(value = "/manager/get/all/finish/order",method = RequestMethod.POST)
    public Map<String,Object> getAllFinishedOrderByDate(@RequestBody AccountAndDate accountAndDate) {
        Map<String, Object> map = new HashMap<>();
        if(accountAndDate.getSelectedDay()!=null) {
            Date selectedDay = new Date(accountAndDate.getSelectedDay());
            List<FinishOrder> orders = manageService.getAllFinishOrderByDate(selectedDay);
            List<String> customers = new ArrayList<>();
            if (orders != null) {
                for (FinishOrder order : orders) {
                    Customer customer = manageService.getCustomerInfoById(order.getCustomerId());
                    customers.add(customer.getCustomerName());
                }
                map.put("status", 0);
                map.put("customers", customers);
                map.put("orders", orders);
            } else {
                map.put("status", 1);
            }
        }else{
            map.put("status", 1);
        }
        return map;
    }

    /**
     * 获取当前所有进行中订单
     * @return
     */
    @RequestMapping(value = "/manager/get/all/doing/order",method = RequestMethod.GET)
    public Map<String,Object> getAllDoingOrder(){
        Map<String, Object> map = new HashMap<>();
        List<DoingOrder> orders=manageService.getAllDoingOrder();
        if(orders!=null){
            List<String> customers = new ArrayList<>();
            for (DoingOrder order : orders) {
                Customer customer = manageService.getCustomerInfoById(order.getCustomerId());
                customers.add(customer.getCustomerName());
            }
            map.put("status", 0);
            map.put("customers", customers);
            map.put("orders", orders);
        }else{
            map.put("status",1);
        }
        return map;
    }

    /**
     * 获取当前所有新下订单
     * @return
     */
    @RequestMapping(value = "/manager/get/all/new/order",method = RequestMethod.GET)
    public Map<String,Object> getAllNewOrder(){
        Map<String, Object> map = new HashMap<>();
        List<NewOrder> orders=manageService.getAllNewOrder();
        if(orders!=null){
            List<String> customers = new ArrayList<>();
            for (NewOrder order : orders) {
                Customer customer = manageService.getCustomerInfoById(order.getCustomerId());
                customers.add(customer.getCustomerName());
            }
            map.put("status", 0);
            map.put("customers", customers);
            map.put("orders", orders);
        }else{
            map.put("status",1);
        }
        return map;
    }

}
